package hospital.action;

import hospital.model.EmpInfoModel;
import hospital.model.RecordDetailModel;
import hospital.service.FinancialService;
import hospital.util.HospitalUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class FinancialOperation extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5530323411095279788L;
	private FinancialService financialService;
	private HospitalUtil hospitalUtil;

	private String appointDocId;
	private String appointDocFirstname;
	private String appointDocLastname;
	private List<EmpInfoModel> appointDocSearchResult;

	private String startDate;
	private String endDate;
	private String yearStart;
	private String monthStart;
	private String dayStart;
	private String yearEnd;
	private String monthEnd;
	private String dayEnd;
	private String recordListDocId;
	private List<RecordDetailModel> recordDetailList;

	private String recordPatientId;
	private String recordDocId;

	private boolean flag;
	private String message;

	private String origPassword;
	private String newPassword;
	private String confirmedNewPassword;

	@Override
	public void prepare() throws Exception {
		clearActionErrors();
		clearFieldErrors();
	}

	public String statistics() {
		return SUCCESS;
	}

	public String goBackToFinancialHome() {
		return SUCCESS;
	}

	public String docSearch() {
		String wrappedUserId = appointDocId.trim();
		long userId = -1;
		if (!wrappedUserId.equals("")) {
			userId = hospitalUtil.deIdWrapper(wrappedUserId, (byte) 1);
		}
		String firstname = appointDocFirstname.trim();
		String lastname = appointDocLastname.trim();
		if (userId == -1 && firstname.equals("") && lastname.equals("")) {
			appointDocSearchResult.clear();
			return SUCCESS;
		}
		appointDocSearchResult = financialService.getDocResult(userId, firstname, lastname);
		Iterator<EmpInfoModel> it = appointDocSearchResult.iterator();
		EmpInfoModel tmp = new EmpInfoModel();
		while (it.hasNext()) {
			tmp = it.next();
			userId = tmp.getUserId();
			tmp.setUserId(hospitalUtil.idWrapper(userId, (byte) 1));
		}
		return SUCCESS;
	}

	public String recordList() {
		String wrappedUserId = recordListDocId.trim();
		long userId = -1;
		if (!wrappedUserId.equals("")) {
			userId = hospitalUtil.deIdWrapper(wrappedUserId, (byte) 1);
		}
		recordDetailList = financialService.getRecordDetailList(userId);
		Iterator<RecordDetailModel> it = recordDetailList.iterator();
		RecordDetailModel tmp = new RecordDetailModel();
		long patientId, doctorId;
		while (it.hasNext()) {
			tmp = it.next();
			patientId = tmp.getPatientId();
			tmp.setPatientId(hospitalUtil.idWrapper(patientId, (byte) 0));
			doctorId = tmp.getDoctorId();
			tmp.setDoctorId(hospitalUtil.idWrapper(doctorId, (byte) 1));
		}
		return SUCCESS;
	}

	public String searchRecord() {
		flag = true;
		message = "";
		String wrappedDocId = recordDocId.trim();
		long docId = -1;
		if (!wrappedDocId.equals("")) {
			docId = hospitalUtil.deIdWrapper(wrappedDocId, (byte) 1);
		}
		if (docId == -1) {
			flag = false;
			message += "Doctor id cannot be empty for advanced search.<br />";
		}
		String wrappedPatientId = recordPatientId.trim();
		long patientId = -1;
		if (!wrappedPatientId.equals("")) {
			patientId = hospitalUtil.deIdWrapper(wrappedPatientId, (byte) 0);
		}
		if (patientId == -2) {
			flag = false;
			message += "Patient id is not valid.<br />";
		}
		long recordTimeStart = -1;
		if (!startDate.trim().equals("")) {
			Calendar cStart = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
			Date parsedDate = null;
			try {
				parsedDate = sdf.parse(startDate + " " + "00:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (parsedDate != null) {
				cStart.setTime(parsedDate);
			}
			recordTimeStart = cStart.getTimeInMillis();
		}
		long recordTimeEnd = -1;
		if (!endDate.trim().equals("")) {
			Calendar cEnd = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm");
			Date parsedDate = null;
			try {
				parsedDate = sdf.parse(endDate + " " + "00:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (parsedDate != null) {
				cEnd.setTime(parsedDate);
			}
			recordTimeEnd = cEnd.getTimeInMillis() + 86400000;
		}
		if (recordTimeStart != -1 && recordTimeEnd != -1 && recordTimeStart >= recordTimeEnd) {
			flag = false;
			message += "The start time should not come after the end time.<br />";
		}
		if (false == flag) {
			return SUCCESS;
		}
		recordDetailList = financialService.searchRecord(docId, patientId, recordTimeStart, recordTimeEnd);
		Iterator<RecordDetailModel> it = recordDetailList.iterator();
		RecordDetailModel tmp = new RecordDetailModel();
		long wpatientId, wdoctorId;
		while (it.hasNext()) {
			tmp = it.next();
			wpatientId = tmp.getPatientId();
			tmp.setPatientId(hospitalUtil.idWrapper(wpatientId, (byte) 0));
			wdoctorId = tmp.getDoctorId();
			tmp.setDoctorId(hospitalUtil.idWrapper(wdoctorId, (byte) 1));
		}
		flag = true;
		return SUCCESS;
	}

	public String changePassword() {
		String encryptedPw = financialService.checkPassword();
		if (encryptedPw.equals(hospitalUtil.encrypt(origPassword))) {
			if (newPassword.length() < 6) {
				flag = false;
				message = "Password needs to be at least 6 characters long";
				return SUCCESS;
			}
			if (newPassword.equals(confirmedNewPassword)) {
				boolean result = financialService.changePassword(newPassword);
				if (result) {
					flag = true;
					message = "Change password successfully.";
					return SUCCESS;
				} else {
					flag = false;
					message = "Fail to change password.";
					return SUCCESS;
				}
			} else {
				flag = false;
				message = "New passwords do not match.";
				return SUCCESS;
			}
		} else {
			flag = false;
			message = "Incorrect password.";
			return SUCCESS;
		}

	}

	public String getAppointDocId() {
		return appointDocId;
	}

	public void setAppointDocId(String appointDocId) {
		this.appointDocId = appointDocId;
	}

	public String getAppointDocFirstname() {
		return appointDocFirstname;
	}

	public void setAppointDocFirstname(String appointDocFirstname) {
		this.appointDocFirstname = appointDocFirstname;
	}

	public String getAppointDocLastname() {
		return appointDocLastname;
	}

	public void setAppointDocLastname(String appointDocLastname) {
		this.appointDocLastname = appointDocLastname;
	}

	public List<EmpInfoModel> getAppointDocSearchResult() {
		return appointDocSearchResult;
	}

	public void setAppointDocSearchResult(List<EmpInfoModel> appointDocSearchResult) {
		this.appointDocSearchResult = appointDocSearchResult;
	}

	public FinancialService getFinancialService() {
		return financialService;
	}

	public void setFinancialService(FinancialService financialService) {
		this.financialService = financialService;
	}

	public String getYearStart() {
		return yearStart;
	}

	public void setYearStart(String yearStart) {
		this.yearStart = yearStart;
	}

	public String getMonthStart() {
		return monthStart;
	}

	public void setMonthStart(String monthStart) {
		this.monthStart = monthStart;
	}

	public String getDayStart() {
		return dayStart;
	}

	public void setDayStart(String dayStart) {
		this.dayStart = dayStart;
	}

	public String getYearEnd() {
		return yearEnd;
	}

	public void setYearEnd(String yearEnd) {
		this.yearEnd = yearEnd;
	}

	public String getMonthEnd() {
		return monthEnd;
	}

	public void setMonthEnd(String monthEnd) {
		this.monthEnd = monthEnd;
	}

	public String getDayEnd() {
		return dayEnd;
	}

	public void setDayEnd(String dayEnd) {
		this.dayEnd = dayEnd;
	}

	public String getRecordListDocId() {
		return recordListDocId;
	}

	public void setRecordListDocId(String recordListDocId) {
		this.recordListDocId = recordListDocId;
	}

	public List<RecordDetailModel> getRecordDetailList() {
		return recordDetailList;
	}

	public void setRecordDetailList(List<RecordDetailModel> recordDetailList) {
		this.recordDetailList = recordDetailList;
	}

	public String getRecordPatientId() {
		return recordPatientId;
	}

	public void setRecordPatientId(String recordPatientId) {
		this.recordPatientId = recordPatientId;
	}

	public String getRecordDocId() {
		return recordDocId;
	}

	public void setRecordDocId(String recordDocId) {
		this.recordDocId = recordDocId;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public HospitalUtil getHospitalUtil() {
		return hospitalUtil;
	}

	public void setHospitalUtil(HospitalUtil hospitalUtil) {
		this.hospitalUtil = hospitalUtil;
	}

	public String getOrigPassword() {
		return origPassword;
	}

	public void setOrigPassword(String origPassword) {
		this.origPassword = origPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmedNewPassword() {
		return confirmedNewPassword;
	}

	public void setConfirmedNewPassword(String confirmedNewPassword) {
		this.confirmedNewPassword = confirmedNewPassword;
	}

}
